#include "Button.h"
#include "Camera.h"
#include "Physics.h"

// Constructor
Button::Button(void)
{
	objectID = "Button";

	defaultActive = false;
	isToggled = false;
	hasHandledCollision = false;

	activeImageID = -1;
	inactiveImageID = -1;
}

// Destructor
Button::~Button(void)
{
}

void Button::Update(float deltaTime)
{
	if(hasHandledCollision == true)
		ToggleOn();
	else
		ToggleOff();

	hasHandledCollision = false;
}
void Button::Render(void) const
{
	int id = defaultActive != isToggled ? activeImageID : inactiveImageID;

	CSGD_TextureManager::GetInstance()->Draw(id, 
		(int)(positionX - Camera::GetInstance()->OffsetX()), 
		(int)(positionY - Camera::GetInstance()->OffsetY()));
}
void Button::HandleCollision(GameObject& other, CollisionInfo& info)
{
	if(other.GetObjectID() == "Player")
	{
		hasHandledCollision = true;
		//other.SetPosY(other.GetPosY() - 0.01f);
	}
	else if(other.GetObjectID() == "Wood Box")
		hasHandledCollision = true;
	else if(other.GetObjectID() == "Metal Box")
		hasHandledCollision = true;
	else if(other.GetObjectID() == "Radiation Barrel")
		hasHandledCollision = true;

		//Pushes objects out
		//if(info.Side == CollisionSide::BOTTOM)
		//	other.SetPosY((other.GetPosY() - (info.Overlap.bottom - info.Overlap.top)) + 1);
		//if(info.Side == CollisionSide::TOP)
		//	other.SetPosY( other.GetPosY() + (info.Overlap.bottom - info.Overlap.top));
		//if(info.Side == CollisionSide::LEFT)
		//	other.SetPosY( other.GetPosY() - (info.Overlap.bottom - info.Overlap.top));
		//if(info.Side == CollisionSide::RIGHT)
		//	other.SetPosY( other.GetPosY() - (info.Overlap.bottom - info.Overlap.top));
	


}

// SetActiveImageID
//	- Sets the id for the image the button will render
//	  when it is active.
void Button::SetActiveImageID(int id)
{
	activeImageID = id;
}

// SetInactiveImageID
//	- Sets the id for the image the button will render
//	  when it is inactive.
void Button::SetInactiveImageID(int id)
{
	inactiveImageID = id;
}

// SetDefaultState
//	- Sets whether or not the button is active by default
//	  of not.
//	- If 'active' is TRUE, the button is active by default.
//	- If 'active' is FALSE, the button is inactive by default.
void Button::SetDefaultState(bool active)
{
	defaultActive = active;
}

// ToggleOn
//	- Toggles the button to its non-default state.
//	- Example: If the button is active by default, calling this
//	  this function will set the button to inactive.
//	- Sends an event say the button was toggled to an active/inactive state.
//	  The event is sent only once. To send the event again, the button
//	  must be toggled off and back on again.
void Button::ToggleOn(void)
{
	if(isToggled == false)
	{
		isToggled = true;

		if(defaultActive == false)
			CSGD_EventSystem::GetInstance()->SendEvent("ButtonActivated", nullptr, nullptr, (void*)this);
		else
			CSGD_EventSystem::GetInstance()->SendEvent("ButtonDeactivated", nullptr, nullptr, (void*)this);
	}
}

// ToggleOff
//	- Toggles the button back to its default state.
//	- Example: If the button is active by default, calling this
//	  this function will set the button to active.
//	- Sends an event say the button was toggled to an active/inactive state.
//	  The event is sent only once. To send the event again, the button
//	  must be toggled on and back off again.
void Button::ToggleOff(void)
{
	if(isToggled == true)
	{
		isToggled = false;

		if(defaultActive == false)
			CSGD_EventSystem::GetInstance()->SendEvent("ButtonDeactivated", nullptr, nullptr, (void*)this);
		else
			CSGD_EventSystem::GetInstance()->SendEvent("ButtonActivated", nullptr, nullptr, (void*)this);
	}
}
